package org.xiaoxin.onecoupon.merchant.admin.common.log;

import cn.hutool.core.util.StrUtil;
import com.mzt.logapi.service.IParseFunction;
import jodd.util.StringUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.xiaoxin.onecoupon.merchant.admin.common.enums.DiscountTargetEnum;
import org.xiaoxin.onecoupon.merchant.admin.common.enums.DiscountTypeEnum;

import java.util.List;

/**
 * 处理日志框架中优惠对象和优惠券类型都是 type 值的自定义函数
 * @author xiaoxin
 * 2025/4/15
 */
@Component
public class CommonEnumParseFunction implements IParseFunction {
   public  static  final  String DISCOUNT_TARGET_ENNUM_CLASS_NAME=DiscountTargetEnum.class.getSimpleName();
   public  static  final  String DISCOUNT_TYPE_ENUM_CLASS_NAME= DiscountTypeEnum.class.getSimpleName();

   @Override
   public String functionName() {
      return "COMMON_ENUM_PARSE";
   }

   @Override
   public String apply(Object value) {
      List<String> parts = StrUtil.split(value.toString(), "_");
      if (parts.size()!=2){
         throw  new IllegalArgumentException("格式错误，需要 '枚举类_具体值' 的形式。");
      }
      String enumClassName = parts.get(0);
      int enumValue=Integer.parseInt(parts.get(1));
      return  findEnumValueByType(enumClassName,enumValue);

   }

   private String findEnumValueByType(String enumClassName, int enumValue) {
      if (DISCOUNT_TARGET_ENNUM_CLASS_NAME.equals(enumClassName)){
         return  DiscountTargetEnum.findValueByType(enumValue);
      } else if (DISCOUNT_TYPE_ENUM_CLASS_NAME.equals(enumClassName)) {
         return  DiscountTypeEnum.findValueByType(enumValue);
      }else {
         throw new IllegalArgumentException("未知的枚举类名: " + enumClassName);
      }
   }
}
